/**create by framework at 2019年07月23日 15:33:44**/
package com.star.truffle.module.fzjz.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.star.truffle.core.StarServiceException;
import com.star.truffle.core.web.ApiCode;
import com.star.truffle.core.web.ApiResult;
import com.star.truffle.module.fzjz.dto.req.FzjzUserInfoRequestDto;
import com.star.truffle.module.fzjz.dto.res.FzjzUserInfoResponseDto;
import com.star.truffle.module.fzjz.properties.FzjzCfg;
import com.star.truffle.module.fzjz.service.FzjzUserInfoService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/fzjzUserInfo")
public class FzjzUserInfoController {

  @Autowired
  private FzjzUserInfoService fzjzUserInfoService;
  @Autowired
  private FzjzCfg fzjzCfg;

  @RequestMapping(value = "/editBefore/{userId}", method = RequestMethod.GET)
  public String editBefore(@PathVariable Long userId, Model model) {
    FzjzUserInfoResponseDto fzjzUserInfoResponseDto = this.fzjzUserInfoService.getByUserId(userId);
    if(null == fzjzUserInfoResponseDto) {
      fzjzUserInfoResponseDto = new FzjzUserInfoResponseDto();
      fzjzUserInfoResponseDto.setUserId(userId);
    }
    model.addAttribute("fzjzUserInfoResponseDto", fzjzUserInfoResponseDto);
    model.addAttribute("areas", fzjzCfg.getAreas());
    return "mgr/fzjzUserInfo/editFzjzUserInfo";
  }

  @ResponseBody
  @RequestMapping(value = "/edit", method = RequestMethod.POST)
  public ApiResult<Void> edit(@Valid @RequestBody FzjzUserInfoRequestDto fzjzUserInfoRequestDto) {
    try {
      fzjzUserInfoService.updateFzjzUserInfo(fzjzUserInfoRequestDto);
      return ApiResult.success();
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }

}